package com.atguigu.gmall.product.controller;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //  创建对象 无返回值
//                CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//                    System.out.println("无返回值.");
//                });
//
//                //  有返回值
//                CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//                    return 1024;
//                });
//
//                System.out.println(voidCompletableFuture.get());
//                System.out.println(integerCompletableFuture.get());

        //  串行化
        //  计算回调    有返回值！
                CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
                            //            int i = 1/0;
                            return 1024;
                        }).thenApplyAsync((s) -> {
                            System.out.println("s:\t" + s);   //  异常
                            return s*2;
                        }).whenComplete((i, t) -> {
                            System.out.println("i:\t" + i);   //  上一个结果
                            System.out.println("t:\t" + t);   //  异常
                        })
                        .exceptionally((f) -> {
                            System.out.println("f:\t" + f);
                            return 404;
                        });

                    //  无返回值
                    CompletableFuture<Void> integerCompletableFuture01 = CompletableFuture.supplyAsync(() -> {
                            //            int i = 1/0;
                        System.out.println("执行一个线程.");
                        return 1024;
                        }).thenAccept((t)->{
                        System.out.println(t);
                    });

                System.out.println(integerCompletableFuture01.get());

        //  并行化：
//        CompletableFuture<String> completableFutureA = CompletableFuture.supplyAsync(() -> "hello");
//
//        CompletableFuture<Void> completableFutureB = completableFutureA.thenAcceptAsync((c) -> {
//            try {
//                TimeUnit.SECONDS.sleep(3);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println(c + "\t:B");
//        });
//
//        CompletableFuture<Void> completableFutureC = completableFutureA.thenAcceptAsync((c) -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println(c + "\t:C");
//        });
//
//        System.out.println(completableFutureB.get());
//        System.out.println(completableFutureC.get());
    }
}
